package senior.MapTest.exer1;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

/**
 * @author shkstart
 * @creat 2022-04-13:45
 */
public class CompareTest {
    public static void main(String[] args) {
        HashMap<Object, Object> map = new HashMap<>();
        Hashtable<Object, Object> table = new Hashtable<>();
        long startMap = System.currentTimeMillis();
        for (int i = 0;i<100000;i++){
            map.put(i,i);
        }
        long endMap = System.currentTimeMillis();
        System.out.println("使用HashMap添加10万个数字花费" + (endMap - startMap) + "ms");
        long startTable = System.currentTimeMillis();
        for (int i = 0;i < 100000;i++){
            table.put(i,i);
        }
        long endTable = System.currentTimeMillis();
        System.out.println("使用HashTable添加10万个数字花费" + (endTable - startTable) + "ms");
        Set<Object> set = map.keySet();
        Iterator<Object> iterator = set.iterator();
        long start = System.currentTimeMillis();
        for (int i = 0;i < 100;i++){
            while (iterator.hasNext()) {
                Integer next = (Integer) iterator.next();
                if(next == (int) (Math.random() * map.size())){
                    iterator.remove();
                }
            }
        }
        long end = System.currentTimeMillis();
        System.out.println("使用HashMap随机删除100个数字花费" + (end - start) + "ms");
        Set<Object> set1 = table.keySet();
        Iterator<Object> iterator1 = set1.iterator();
        long start1 = System.currentTimeMillis();
        for (int i = 0;i < 100;i++){
            while (iterator1.hasNext()) {
                Integer next = (Integer) iterator1.next();
                if(next == (int)(Math.random()*table.size())){
                    iterator1.remove();
                }

            }
        }
        long end1 = System.currentTimeMillis();
        System.out.println("使用HashTable随机删除100个数字花费" + (end1 - start1) + "ms");


    }
}
